package com.study.seckill.web.page;

import com.study.dto.UserDTO;
import com.study.seckill.entity.CourseSeckill;
import com.study.seckill.entity.CourseSeckillOrder;
import com.study.seckill.service.CourseSeckillOrderService;
import com.study.seckill.service.CourseSeckillService;
import com.study.seckill.service.UserService;
import com.study.seckill.vo.CourseSeckillDetailVO;
import com.study.seckill.vo.SeckillDetailInfo;
import com.study.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigInteger;
import java.util.List;

/**
 * @ClassName: CourseSeckillOrderController
 * @Author: wYin
 * @Date: 2022/3/10 14:50
 */
@Controller
@RequestMapping("/page/courseSeckillOrder")
public class CourseSeckillOrderController {
    @Autowired
    private CourseSeckillOrderService courseSeckillOrderService;

    @Autowired
    private CourseSeckillService courseSeckillService;

    @Autowired
    private UserService userService;

    @GetMapping("/getOrderList")
    public String getOrderList(Model model){
        List<CourseSeckillOrder> list = courseSeckillOrderService.list();
        model.addAttribute("data",list);
        return "order_list";
    }

    @PostMapping("/add")
    public String addOrder(CourseSeckillOrder courseSeckillOrder, Model model, HttpSession httpSession){
        //todo 从session中得到用户的id
        //执行添加
        CourseSeckillOrder result = courseSeckillOrderService.addOrder(courseSeckillOrder, 1);
        if(result==null){
            CourseSeckillDetailVO seckillInfoById = courseSeckillService.getSeckillInfoById(courseSeckillOrder.getSeckillId());
            model.addAttribute("data",seckillInfoById);
            return "item";
        }
        //查询秒杀信息
        CourseSeckill seckill = courseSeckillService.getById(result.getSeckillId());
        //查询用户信息
        ResponseResult<UserDTO> user = userService.getById(Integer.valueOf(result.getUserId()));
        SeckillDetailInfo seckillDetailInfo = new SeckillDetailInfo();
        seckillDetailInfo.setCourseSeckill(seckill);
        seckillDetailInfo.setUserDTO(user.getData());
        seckillDetailInfo.setCourseSeckillOrder(result);
        model.addAttribute("data",seckillDetailInfo);//显示订单详情
        return "orderDetail";
    }

    @PostMapping("/updateOrderStatus")
    public String updateOrderStatus(CourseSeckillOrder courseSeckillOrder,Model model,HttpSession session){
        //根据订单的信息执行操作（session中得到用户的信息）
        Boolean result = courseSeckillOrderService.updateOrderStatus(courseSeckillOrder, 1);
        if(!result){
            CourseSeckillOrder order = courseSeckillOrderService.getById(courseSeckillOrder.getId());
            Integer userId = Integer.valueOf(courseSeckillOrder.getUserId());
            ResponseResult<UserDTO> user = userService.getById(userId);
            CourseSeckill seckill = courseSeckillService.getById(courseSeckillOrder.getSeckillId());
            SeckillDetailInfo seckillDetailInfo = new SeckillDetailInfo();
            seckillDetailInfo.setCourseSeckill(seckill);
            seckillDetailInfo.setUserDTO(user.getData());
            seckillDetailInfo.setCourseSeckillOrder(order);
            model.addAttribute("data",seckillDetailInfo);
            return "orderDetail";
        }
        return "result";
    }
}
